// ignore_for_file: avoid_unnecessary_containers

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGNavigationBar.dart';

class QGMineHasAuthentPage extends StatefulWidget {
  const QGMineHasAuthentPage({super.key});

  @override
  State<QGMineHasAuthentPage> createState() => _QGMineHasAuthentPageState();
}

class _QGMineHasAuthentPageState extends State<QGMineHasAuthentPage> {
  Map<String, dynamic>? userInfo;

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    onRefresh();
  }

  onRefresh() {
    Network.get(url_u_getMergedUserInfo).then((res) async {
      if (!res.success) {
        toast(res.msg);

        //处理token失效的情形
        if (!User.isLogin) {
          await Future.delayed(const Duration(seconds: 2));
          Navigator.of(context).pushNamed(PageLogin).then((value) {
            if (!User.isLogin) {
              eventBus.fire(EventMainTabSelectIndex(index: 0));
            }
          });
        }
        return;
      }

      userInfo = res.data["user_info"];
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Color(0xFFF8F8F6),
      body: Stack(
        children: [
          Positioned(
            top: 0.rpx,
            child: buildImage(),
          ),
          SizedBox(
             height: kScreenHeight,
              child: Column(children: [
                 buildTop(),
                 SizedBox(height: 30,),
                 buildSuccess(),
                 SizedBox(height: 30.rpx),
                 buildAuthent(),
                  SizedBox(height: 20.rpx),
                  buildInfoSafe(),
              ])),
                 
          // Positioned(
          //   left: 0.rpx,
          //   bottom: 64.rpx,
          //   child: buildCommitment(context),
          // ),

        ],
      ),
    );
  }

  buildImage() {
    return QGImage(asset: 'mine_authent_bg', width: kScreenWidth);
  }

  buildTop() {
    return Container(
        // color: Colors.white,
        child: Column(
          children: [
            QGNavigationBar(
              title: '我的认证',
            ),
          ],
        ));
  }

  buildSuccess() {
    return Stack(children: [
       ClipRRect(
              borderRadius: BorderRadius.circular(40.rpx),
              child: QGImage(
                  width: 80.rpx, height: 80.rpx, url: userInfo?["avatar"]??""),
            ),
      Positioned(
        bottom: 0,
        right: 0,
        child: 
           QGImage(asset: 'mine_authent_ok',width: 30.rpx),
      )


    ]);
  }

  buildText(String content,int size, FontWeight weight, Color color) {
    return Text(
      content,
      textAlign: TextAlign.center,
      style: TextStyle(fontSize: size.rpx, fontWeight: weight, color: color),
    );
  }

  buildCommitment(context) {
    return GestureDetector(
        onTap: () {
          Navigator.of(context).pushNamed(pageMineCommitment);
        },
        child: Container(
          width: kScreenWidth,
          alignment: Alignment.center,
          child: Text(
            '《APP个人信息保护自律承诺书》',
            style: TextStyle(
                fontSize: 12.rpx,
                fontWeight: FontWeight.bold,
                color: Color(0xFF5E85E3)),
          ),
        )
    );
  }

  buildAuthent() {
    return Container(
        margin: EdgeInsets.only(left: 15.rpx, right: 15.rpx),
        padding: EdgeInsets.all(15.rpx),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8.rpx),
            color: Color(0xFF232A32)),
        child: Row(
          children: [
            // ClipRRect(
            //   borderRadius: BorderRadius.circular(29.rpx),
            //   child: QGImage(
            //       width: 57.rpx, height: 57.rpx, url: userInfo?["avatar"]),
            // ),
            // SizedBox(
            //   width: 15.rpx,
            // ),
            Expanded(
                child: Container(
              child: buildInfo(),
            ))
          ],
        ));
  }

  buildInfo() {
    return Column(
      children: [
        buildName(),
         buildIdentify(),
      ],
    );
  }

  buildName() {
    var name = userInfo?["Name"] ?? "";
    
    if (name is String && name.isEmpty) {
      name = "";
    }

    return Container(
      padding: EdgeInsets.only(bottom: 15.rpx),
      decoration: BoxDecoration(
          // border: Border(
          //     bottom: BorderSide(
          //       width: 0.5.rpx,
          //      color: Color(0xFFEAEAEA)))
               ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          buildText(
              '姓名', 13, FontWeight.normal,Color(0XFFFFFFFF)),
          buildText(name, 14, FontWeight.normal, Color(0XFFFFFFFF))
        ],
      ),
    );
  }

  buildIdentify() {
    var cardNo = userInfo?["CardNo"] ?? "";
    if (cardNo is String && cardNo.isEmpty) {
      cardNo = "";
    }

    return Container(
      padding: EdgeInsets.only(top: 15.rpx),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          buildText(
              '身份证号', 13, FontWeight.normal,Color(0XFFFFFFFF)),
          buildText(cardNo, 14, FontWeight.normal, Color(0XFFFFFFFF))
        ],
      ),
    );
  }

  buildInfoSafe() {
    return Container(
      margin: EdgeInsets.only(top: 20.rpx),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          QGImage(asset: 'mine_authent_safe',width: 100.rpx),
          // SizedBox(
          //   width: 10.rpx,
          // ),
          // buildText(
          //     '安全保障中', 10, FontWeight.bold,Color(0XFFFFFFFF)),
        ],
      ),
    );
  }
}
